import 'package:flutter/material.dart';
import '../main.dart';  // 导入 main.dart 以使用 navigatorKey

class Loading {
  static bool _isVisible = false;

  static void show(BuildContext context) {
    if (_isVisible) return;
    _isVisible = true;

    showDialog(
      context: context,
      barrierDismissible: false,
      barrierColor: Colors.black26,
      builder: (context) => WillPopScope(
        onWillPop: () async => false,
        child: Center(
          child: Container(
            width: 100,
            height: 100,
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
            ),
            padding: const EdgeInsets.all(24),
            child: const CircularProgressIndicator(),
          ),
        ),
      ),
    );
  }

  static void hide() {
    if (!_isVisible) return;
    _isVisible = false;
    
    // 使用 navigatorKey 来获取全局 context
    final context = navigatorKey.currentContext;
    if (context != null) {
      Navigator.of(context, rootNavigator: true).pop();
    }
  }
} 